/**
 * All rights Reserved
 *
 * @Title: SolrDataHandler.java
 * @Package com.amigo.online.provider.sync.abs
 * @Description: (用一句话描述该文件做什么)
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月14日 下午6:26:11
 * @version V1.0
 * @Copyright: 2018  All rights reserved.
 */
package com.amigo.online.provider.sync.abs;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.ClassNameConvertEnum;

/**
 * @version: V 1.0.0
 * @Description: （对类进行功能描述） 
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月14日 下午6:26:11  
 *
 */
@Service("solrDataHandler")
public class SolrDataHandler implements DataHandlerService {

    @Autowired
    SolrService solrService;

    private static final String SOLR_PARTUPDATE_OPERATE_SET = "set";

    private static final int SOLR_PARTUPDATE_OPERATE_VERSION_NUMBER = 1;

    private static final String SOLR_PARTUPDATE_OPERATE_VERSION = "_version_";
    /**
     * 通用logger记录
     */
    protected static final Logger logger = LoggerFactory.getLogger(SolrDataHandler.class);

    /**
     * <p>Title: updateHandle</p>
     * <p>Description: </p>
     * @param newObject
     * @param columnSet
     * @param hadlerMap
     * @see com.amigo.online.provider.sync.abs.DataHandlerService#updateHandle(com.alibaba.fastjson.JSONObject, java.util.Set, com.amigo.online.provider.sync.abs.ColumnHandleMap)
     */
    @Override
    public void updateHandle(JSONObject newObject, Set<String> columnSet, ColumnHandleMap hadlerMap) {
        SolrInputDocument document = new SolrInputDocument();
        Long id = newObject.getLong(hadlerMap.getId());
        document.addField(hadlerMap.getId(), id);
        boolean updateFlag = false;
        Map<String, String> projectMap = hadlerMap.getProjectMap();
        Map<String, String> classMap = hadlerMap.getClassMap();
        for (String temp : columnSet) {
            if (hadlerMap.getColumnList().contains(temp)) {
                String solrColumnName = projectMap.get(temp);
                String solrClassName = classMap.get(temp);
                if (StringUtils.isEmpty(solrColumnName)) {
                    solrColumnName = temp;
                }
                Map<String, Object> partialUpdate = new HashMap<>(1);
                if (StringUtils.isEmpty(solrClassName)) {
                    partialUpdate.put(SOLR_PARTUPDATE_OPERATE_SET, newObject.get(temp));
                } else {
                    try {
                        Class<?> clazz = Class.forName(ClassNameConvertEnum.getColumnName(solrClassName));
                        partialUpdate.put(SOLR_PARTUPDATE_OPERATE_SET, newObject.getObject(temp, clazz));
                    } catch (ClassNotFoundException e) {
                        logger.info("更新获取中" + temp + "字段类型失败！");
                    }
                }
                document.addField(solrColumnName, partialUpdate);
                updateFlag = true;
            }
        }
        if (updateFlag == true) {
            document.addField(SOLR_PARTUPDATE_OPERATE_VERSION, SOLR_PARTUPDATE_OPERATE_VERSION_NUMBER);
            solrService.updateObject(document, hadlerMap.getCollection());
        }
    }

    /**
     * <p>Title: insertHandle</p>
     * <p>Description: </p>
     * @param newObject
     * @param hadlerMap
     * @see com.amigo.online.provider.sync.abs.DataHandlerService#insertHandle(com.alibaba.fastjson.JSONObject, com.amigo.online.provider.sync.abs.ColumnHandleMap)
     */
    @Override
    public void insertHandle(JSONObject source, ColumnHandleMap hadlerMap) {
        SolrInputDocument document = new SolrInputDocument();
        Map<String, String> projectMap = hadlerMap.getProjectMap();
        Map<String, String> classMap = hadlerMap.getClassMap();
        for (String temp : hadlerMap.getColumnList()) {
            String solrFieldName = projectMap.get(temp);
            String solrClassName = classMap.get(temp);

            if (StringUtils.isEmpty(solrFieldName)) {
                solrFieldName = temp;
            }
            if (StringUtils.isEmpty(solrClassName)) {
                document.addField(solrFieldName, source.get(temp));
            } else {
                try {
                    Class<?> clazz = Class.forName(ClassNameConvertEnum.getColumnName(solrClassName));
                    document.addField(solrFieldName, source.getObject(temp, clazz));
                } catch (ClassNotFoundException e) {
                    logger.info("获取{}中字段类型失败！", hadlerMap.getCollection());
                }
            }
        }
        solrService.insertObject(document, hadlerMap.getCollection());
    }


}
